<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div id="app">
        <lay_head :msgs="msg" @test="add"></lay_head>
    </div>
    <script>
        Vue.component("lay_head",{
           props:['msgs','test'],
            template:`
                <div>
                     <lay_child></lay_child>
                    <button @click="del">按钮</button>
                </div>         
            `,
            methods:{
                del(){
                   // this.$emit("test",{name:'周杰伦',age:18})
                   this.$parent.$emit("test",{name:'周杰伦',age:18})
                }
            },
            components:{ //孙子
                "lay_child":{
                    template:"<div>孙子<button @click='childText'>自测试</button></div>",
                    methods:{
                        childText(){
                            this.$parent.$parent.$emit("test",{name:"周杰伦",age:20})
                            console.log(this.$root.name);
                            
                        }
                    }
                }
            }
        })
        new Vue({
            el:"#app",
            data:{
                msg:"嘻嘻嘻嘻嘻"

            },
            methods:{
                add(){

                }
            },
           mounted() { //$root 根元素
               this.$on("test",(val)=>{
                console.log(val);
               })
               this.$root.name="刘德华"
           },
        })
    </script>
    
</body>
</html>